package srdb.migration;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;

import srdb.migration.Util.IUIOperation;
import srdb.migration.common.MTKMetaData;


public class TableParser {
    /**
     * 调用专门的解析处理类，将info_tables.txt文件分析
     *
     * @param sourceMD
     * @throws IOException
     */
    public static void parseOra(MTKMetaData sourceMD) throws IOException {
        File splitFile = new File("resource/migration/info_tables.txt");
        List<String> lines = FileUtils.readLines(splitFile);
        // TODO
        StringBuffer emptyTablesSB = new StringBuffer();
        StringBuffer notEmptyTablesSB = new StringBuffer();
        StringBuffer allTablesSB = new StringBuffer();
        int morekw = 0;
        int lesskw = 0;
        int lessbw = 0;
        for (String tableInfo : lines) {
            if (tableInfo != null && !tableInfo.isEmpty()
                    && !tableInfo.trim().equals("")) {
                String[] infoArr = tableInfo.split("\t");
                String tName = infoArr[0];
                int rows = infoArr[1] != null ? Integer.parseInt(infoArr[1])
                        : 0;
                if (rows != 0) {
                    if (rows / 10000000 > 0) {
                        morekw++;
                    } else if (rows / 1000000 > 0) {
                        lesskw++;
                    } else {
                        lessbw++;
                    }
                    notEmptyTablesSB.append(tName).append("\n");
                } else {
                    emptyTablesSB.append(tName).append("\n");
                }
                allTablesSB.append(tName).append("\n");
            }

        }
        System.out.println("千万以上的表有 " + morekw + " 个");
        System.out.println("千万以下的表有 " + lesskw + " 个");
        System.out.println("百万以下的表有 " + lessbw + " 个");
        notEmptyTablesSB.deleteCharAt(notEmptyTablesSB.lastIndexOf("\n"));
        emptyTablesSB.deleteCharAt(emptyTablesSB.lastIndexOf("\n"));
        allTablesSB.deleteCharAt(emptyTablesSB.lastIndexOf("\n"));

        File tables = new File("resource/migration/tables.txt");
        File emptytables = new File("resource/migration/emptytables.txt");
        File alltables = new File("resource/migration/alltables.txt");
        FileUtils.writeStringToFile(tables, notEmptyTablesSB.toString());
        FileUtils.writeStringToFile(emptytables, emptyTablesSB.toString());
        FileUtils.writeStringToFile(alltables, allTablesSB.toString());
    }

    public static void parseOra(MTKMetaData sourceMD, IUIOperation iuiOperation) throws IOException {
        File splitFile = new File("resource/migration/info_tables.txt");
        List<String> lines = FileUtils.readLines(splitFile);
        // TODO
        StringBuffer emptyTablesSB = new StringBuffer();
        StringBuffer notEmptyTablesSB = new StringBuffer();
        StringBuffer allTablesSB = new StringBuffer();
        int morekw = 0;
        int lesskw = 0;
        int lessbw = 0;
        for (String tableInfo : lines) {
            if (tableInfo != null && !tableInfo.isEmpty()
                    && !tableInfo.trim().equals("")) {
                String[] infoArr = tableInfo.split("\t");
                String tName = infoArr[0];
                int rows = infoArr[1] != null ? Integer.parseInt(infoArr[1])
                        : 0;
                if (rows != 0) {
                    if (rows / 10000000 > 0) {
                        morekw++;
                    } else if (rows / 1000000 > 0) {
                        lesskw++;
                    } else {
                        lessbw++;
                    }
                    notEmptyTablesSB.append(tName).append("\n");
                } else {
                    emptyTablesSB.append(tName).append("\n");
                }
                allTablesSB.append(tName).append("\n");
            }

        }
        iuiOperation.pushLogs("千万以上的表有 " + morekw + " 个");
        iuiOperation.pushLogs("千万以下的表有 " + lesskw + " 个");
        iuiOperation.pushLogs("百万以下的表有 " + lessbw + " 个");
        iuiOperation.pushLogs("空表有:" + (lines.size() - morekw - lesskw - lessbw) + " 个");
        notEmptyTablesSB.deleteCharAt(notEmptyTablesSB.lastIndexOf("\n"));
        emptyTablesSB.deleteCharAt(emptyTablesSB.lastIndexOf("\n"));
        allTablesSB.deleteCharAt(emptyTablesSB.lastIndexOf("\n"));

        File tables = new File("resource/migration/tables.txt");
        File emptytables = new File("resource/migration/emptytables.txt");
        File alltables = new File("resource/migration/alltables.txt");
        FileUtils.writeStringToFile(tables, notEmptyTablesSB.toString());
        FileUtils.writeStringToFile(emptytables, emptyTablesSB.toString());
        FileUtils.writeStringToFile(alltables, allTablesSB.toString());
    }

    public static void main(String[] args) {
        try {
            TableParser.parseOra(null);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
